package com.intellagric.agriecom.service.agriecom_news_type;

import com.github.pagehelper.PageHelper;
import com.intellagric.agriecom.module.agriecom_news_type.NewsTypeResult;
import com.intellagric.agriecom.module.agriecom_news_type.NewsTypeService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.AgriecomNewsTypeMapper;
import com.intellagric.mapper.AgriecomProduceCategoryMapper;
import com.intellagric.pojo.AgriecomNewsType;
import com.intellagric.pojo.AgriecomNewsTypeExample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewsTypeServiceImpl implements NewsTypeService {

    @Autowired
    private AgriecomNewsTypeMapper dao;

    @Autowired
    private AgriecomProduceCategoryMapper cateDao;
    /**
     * 通过id查找
     * @param id
     * @return
     */
    @Override
    public ResponseMessage selectNewsTypeById(String id) {
        AgriecomNewsType type=dao.selectByPrimaryKey(id);
        if(type!=null){
            ResponseMessage msg=new ResponseMessage();
//            msg.add("newsType",JsonUtils.objectToJson(type));
            msg.add("newsType",type);
            msg.setTotal(1);
            return msg;
        }
        return ResponseMessage.fail();
    }

    /**
     * 插入新闻种类
     * @param newsType
     * @return
     */
    @Override
    public ResponseMessage insertNewsType(AgriecomNewsType newsType) {
        int r=dao.insert(newsType);
        if(r==0){
            return ResponseMessage.success("插入失败");
        }
        return ResponseMessage.success();

    }

    /**
     * 删除一条数据（id)
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteNewsType(String id) {
        int r=dao.deleteByPrimaryKey(id);
        if(r==0)
            return ResponseMessage.fail("删除失败");
        return ResponseMessage.success();
    }

    /**
     * 编辑一条数据
     * @param newsType
     * @return
     */
    @Override
    public ResponseMessage editNewsType(AgriecomNewsType newsType) {

        if(newsType!=null){
            AgriecomNewsTypeExample example =new AgriecomNewsTypeExample();
            example.createCriteria().andIdEqualTo(newsType.getId());
            newsType.setUpdateDate(new Date(System.currentTimeMillis()));
            int r=dao.updateByExampleSelective(newsType,example);
            if(r!=0)
                return ResponseMessage.success();

        }
        return ResponseMessage.fail();
    }

    /**
     * 获得分页数据
     * @param page
     * @param rows
     * @return
     */
    @Override
    public LayuiDataGridResult getList(int page, int rows) {
        PageHelper.startPage(page,rows);
        AgriecomNewsTypeExample example=new AgriecomNewsTypeExample();
        example.createCriteria().andIdIsNotNull();
        example.setOrderByClause("sort asc");
        List<AgriecomNewsType> typeList=dao.selectByExample(example);
        List<NewsTypeResult> resultList=new ArrayList<>();
        for (AgriecomNewsType newsType: typeList){
            NewsTypeResult result=new NewsTypeResult(newsType);
            AgriecomNewsType parent=dao.selectByPrimaryKey(newsType.getParentId());
            if(parent!=null)
                result.setParentName(parent.getName());
            resultList.add(result);
        }
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(resultList,this.getAll().size());
        lay.setCode(0);
        lay.setMsg("分页查询成功");
        return lay;
    }

    /**
     * 获取全部数据
     * @return
     */
    @Override
    public List<AgriecomNewsType> getAll() {
        AgriecomNewsTypeExample example=new AgriecomNewsTypeExample();
        example.createCriteria().andIdIsNotNull();
        example.setOrderByClause("sort asc");
        List<AgriecomNewsType> list=dao.selectByExample(example);
        return list;
    }

    /**
     * 根据父id返回列表
     * @param parentID
     * @return
     */
    public LayuiDataGridResult getTypeByParentID(String parentID){
        List<AgriecomNewsType> list=new ArrayList<>();
        AgriecomNewsTypeExample example=new AgriecomNewsTypeExample();
        AgriecomNewsTypeExample.Criteria criteria=example.createCriteria();
        criteria.andIdIsNotNull();
        criteria.andParentIdEqualTo(parentID);
        list=dao.selectByExample(example);
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(list,list.size());
        lay.setMsg("根据父id返回列表");
        return lay;
    }

    /**
     * 从list集合中获取此id的子集
     * @param id
     * @param list
     * @return
     */
    private List<AgriecomNewsType> getChild(String id,List<AgriecomNewsType> list){
        List<AgriecomNewsType> children=new ArrayList<>();

        for(AgriecomNewsType type:list){  //遍历找出列表中此id的子集
            if(type.getParentId()!=null&&!type.getParentId().equals("")) //避免为空
                if(type.getParentId().equals(id))
                    children.add(type);
        }
        //递归设置子集的子集
        for(AgriecomNewsType type:children){
            type.setChildren(getChild(type.getId(),list));
        }
        //递归停止条件
        if(children.size()==0){
            return null;
        }
        return children;
    }

    /**
     * 获取树形列表
     * @return
     */
    public LayuiDataGridResult getTreeList(){
        LayuiDataGridResult lay=new LayuiDataGridResult();
        List<AgriecomNewsType> list=this.getAll();
        List<AgriecomNewsType> rootList=this.getTypeByParentID("0").getData();
        //设置根路径下的子集
        for(AgriecomNewsType type:rootList){
            type.setChildren(this.getChild(type.getId(),list));
        }
        lay.add(rootList,rootList.size());
        lay.setMsg("获取树形列表成功");
        return lay;
    }
}
